/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjut.se1603.tata.dao.impl;

import edu.fjut.se1603.tata.dao.PetDao;
import edu.fjut.se1603.tata.entity.Pet;
import edu.fjut.se1603.tata.util.ConnectMySQL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.security.rsa.RSACore;

/**
 *
 * @author admin
 */
public class PetDaoOracleImpl implements  PetDao{
 
    ConnectMySQL s=new ConnectMySQL();
      Connection conn=s.Connect();
 
    @Override
    public int add(Pet p) {
          
        int r=0;
        try {
            
            PreparedStatement pst=conn.prepareStatement("insert into pet(NAME,PRICE,KIND)VALUE(?,?,?)");
            pst.setString(1,p.getName());
            pst.setInt(2, p.getPrice());
            pst.setString(3, p.getKind());
            r=pst.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
      
        return r;
    }

    @Override
    public Pet selectid(int id) {
        Pet p=null;
        try {
            
            ResultSet rs = conn.createStatement().executeQuery("select * from pet where ID='"+id+"'");
            if (rs.next()) {
                p=new Pet();
                p.setId(rs.getInt("ID"));
                p.setName(rs.getString("NAME"));
                p.setPrice(rs.getInt("PRICE"));
                p.setKind(rs.getString("KIND"));
                p.setSellerid(rs.getInt("SELLERID"));
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return p;
    }

    @Override
    public Pet selectname(String name) {
           
        Pet p=null;
        try {
            
            ResultSet rs = conn.createStatement().executeQuery("select * from pet where NAME='"+name+"'");
            if (rs.next()) {
                p=new Pet();
                p.setId(rs.getInt("ID"));
                p.setName(rs.getString("NAME"));
                p.setPrice(rs.getInt("PRICE"));
                p.setKind(rs.getString("KIND"));
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        return p;     
    }

    @Override
    public Pet selectprice(String price) {
          
         Pet p=null;
        try {
            
            ResultSet rs = conn.createStatement().executeQuery("select * from pet where PRICE='"+price+"'");
            if (rs.next()) {
                p=new Pet();
                p.setId(rs.getInt("ID"));
                p.setName(rs.getString("NAME"));
                p.setPrice(rs.getInt("PRICE"));
                p.setKind(rs.getString("KIND"));
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return p;   }

    public List<Pet> getAllPet() {
           Pet p=null;
           List<Pet> l=new ArrayList<Pet>();
        try {
            //3执行查询
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("select * from pet"); //显示pet表中的信息
            //4处理集
            while(rs.next())
            {
                p=new Pet();
                p.setId(rs.getInt("ID"));
                p.setName(rs.getString("NAME"));
                p.setPrice(rs.getInt("PRICE"));
                p.setKind(rs.getString("KIND"));
                p.setSellerid(rs.getInt("SELLERID"));
                l.add(p);
            }
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        } return l;
    }
   public void delete(int id)
    {
        try {
            String sql="delete from pet where ID=?";
            PreparedStatement pst=conn.prepareStatement(sql);
            pst.setInt(1, id);
            pst.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoOracleImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
}
